<template>
  <div class="login">
    <el-row>
      <el-col :span="8" :offset="8">
        <div class="login-div">
          <p>小U商城后台管理系统</p>
          <!-- model：表单项的数据 -->
          <!-- rules：表单项的检验规则 -->
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名" prop="username">
              <el-input placeholder="请输入内容" v-model="ruleForm.username">
                <template slot="prepend"><i class="el-icon-user"></i></template>
              </el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input placeholder="请输入内容" v-model="ruleForm.password">
                <template slot="prepend"><i class="el-icon-lock"></i></template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="btn1" @click="submitForm('ruleForm')"
                >立即登录</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3~10位",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3~10位",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      // this.$refs.ruleForm  选择表单，目的是为了校验表单
      // validate element自带的方法，用来校验
      this.$refs[formName].validate((valid) => {
        //vaild值为真时表示校验通过
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //真的登录了
    goLogin() {
      // /api/userlogin
      this.$http.post('/userlogin', {
        username: this.ruleForm.username,
        password: this.ruleForm.password
      })
      .then(res => {
        if(res) {
          this.$router.push('/index');
        }
      })
      .catch(err => {
        console.log(err);
      })
    }
  },
};
</script>

<style>
.login{
  width: 100vw;
  height: 100vh;
  background-color: #56a5f1;
}
.login_div{
  height: 200px;
  background-color: #fff;
}
/* 覆盖原来的样式 */
.login .btn1{
  width: 200px;
}
</style>